﻿using client.utils;
using CommunicationProtocol.dto;
using CommunicationProtocol.dto.command.client;
using CommunicationProtocol.dto.command.server;
using server.filters;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static client.utils.Constant;

namespace client.render
{
    internal class FollowingMousePicture: ObjectCreator
    {

        private Form form;

        private PictureBox pictureBox;

        public FollowingMousePicture(Form form):base(form,0,0)
        {
            this.form = form;

            /*
     0： Control.PointToScreen：将指定工作区点的位置计算成屏幕坐标
     1： Control.MousePosition:  鼠标相对于屏左上角的坐标；
     2： this.PointToClient(Control.MousePosition)： 将指定屏幕点的位置计算成工作区坐标 （鼠标相对于窗体工作区的坐标）；
     3： this.Location：窗体左上角相对于屏左上角的坐标；
     4： System.Windows.Forms.Cursor.Position：鼠标相对于屏左上角的坐标，等同于Control.MousePosition；

  */
            form.Invoke(new Action(() => {

                Point point = form.PointToClient(Control.MousePosition);


                base.xLocationOffset = point.X;
                base.yLocationOffset = point.Y;
            }));


           
        }

        protected override void  afterRender(PictureBox pictureBox, BaseObject baseObj)
        {
     
            this.pictureBox = pictureBox;
            form.MouseMove += Form_MouseMove;
            pictureBox.MouseClick += Form_MouseClick;
        }

        /**
         * 使得植物跟随鼠标移动
         */ 
        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            pictureBox.Location = e.Location;
        }

        /*
         用户选择在这个地方把植物给种下去
         */
        private void Form_MouseClick(object senderObj, MouseEventArgs e)
        {

            Point point = form.PointToClient(Control.MousePosition);

            if (!plantArea.Contains(point)) {
                Console.WriteLine($"当前位置{point.X}，{point.Y} 不能创建植物");
                return;
            }

            if (!(senderObj is Control)) {
                throw new ApplicationException("点击的地方不是一个有效控件，不能种植植物");
            }

            int collumn = plantAreaWidth / (point.X - plantAreaXOffset);

            int row = plantAreaHeight / (point.Y - plantAreaYOffset);


            Control sender = (Control)senderObj;

            new InvocationChain(OutBandHolder.OutBand).invoke(
                new PlantAPlant(
                        ((BaseObject)(sender.Tag)).ObjectId,
                        collumn,
                        row
                    )
                );

            form.MouseMove -= Form_MouseMove;
            pictureBox.MouseClick -= Form_MouseClick;
        }

        internal void copyPlantTemplate(PlantBuyableStatusResp command)
        {
            Control[] buyingPlant = form.Controls.Find(command.objectId.ToString(), false);
            
            if (buyingPlant is null || buyingPlant.Length == 0|| !(buyingPlant[0] is PictureBox)) {
                throw new ArgumentException($"未找到id 为 {command.objectId} 的控件");
            }

            PictureBox pictureBox1 = (PictureBox)buyingPlant[0];

            BaseObject targetObj = (BaseObject)pictureBox1.Tag;

            Execute(targetObj);

        }

    }
}
